from zhipuai import ZhipuAI
from tool.tool_register import dispatch_tool, get_lm4_tools
import json
client = ZhipuAI(api_key="6795fbe303878f35292f1aa14414e9a4.kOzmNe7PEk1l6zdK") # 请填写您自己的APIKey
# tools = [
#     {
#         "type": "function",
#         "function": {
#             "name": "query_train_info",
#             "description": "根据用户提供的信息，查询对应的车次",
#             "parameters": {
#                 "type": "object",
#                 "properties": {
#                     "departure": {
#                         "type": "string",
#                         "description": "出发城市或车站",
#                     },
#                     "destination": {
#                         "type": "string",
#                         "description": "目的地城市或车站",
#                     },
#                     "date": {
#                         "type": "string",
#                         "description": "要查询的车次日期",
#                     },
#                 },
#                 "required": ["departure", "destination", "date"],
#             },
#         }
#     }
# ]
tools=get_lm4_tools()
messages = [
    {
        "role": "user",
        "content": "帮我发一条微信消息给老板，告诉他今天太冷了不能准时上班"
    }
]
response = client.chat.completions.create(
    model="glm-4", # 填写需要调用的模型名称
    messages=messages,
    tools=tools,
    tool_choice="auto",
)
print(response.choices[0].message)

def nested_object_to_dict(obj):
    if isinstance(obj, list):
        return [nested_object_to_dict(x) for x in obj]
    if isinstance(obj, dict):
        return {k: nested_object_to_dict(v) for k, v in obj.items()}
    if  obj and type(obj) not in (int, float, str):
        return nested_object_to_dict(vars(obj))
    else:
        return obj
 
print(nested_object_to_dict(response.choices[0].message))
client.close()